import type { AxiosResponse } from 'axios'

import type { IGetConfigParams, IResponseConfig, IResponseTable } from '@/types/api/index.js'
import { ElMessage } from 'element-plus'
import { defineStore } from 'pinia'
import { getConfigAPI, getDataByIDAPI } from '@/api/common.js'

const useDataStore = defineStore('data', {
  state: () => {
    return {
      test: 'test',
    }
  },
  actions: {
    // 获取查询表单&表头等信息
    async getConfigData(args: IGetConfigParams[]) {
      const res = await getConfigAPI(args).catch(err => err) as AxiosResponse<IResponseConfig>
      if (res.success) {
        // 只有成功的请求才会返回数据 失败没有返回值
        return res.data
      }
      else {
        ElMessage.error(res.message)
      }
    },

    // 获取对应ID数据
    async getDataByID(args: any) {
      const res = await getDataByIDAPI(args).catch(err => err) as AxiosResponse<IResponseTable>
      if (res.success) {
        return res.data
      }
      else {
        ElMessage.error(res.message)
      }
    },

  },
  getters: {
    theme: state => state.test,
  },

})
export default useDataStore
